<?
//Cache 0.0.4
class cache {
    private $file;
    private $time;
    private $cache=true;
    //Creatign bufer if it need
    public function __construct($file, $time=0){
        $e=explode('/', $file);
        $f='cache/';
        $c=count($e)-1;
        for($i=0; $i<$c; $i++){
            $f.=$e[$i].'/';
            if(!file_exists($f)) mkdir($f);
        }
        $this->file='cache/'.$file.'.chc.html';
        $this->time=$time;
        if(file_exists($this->file)&&(filemtime($this->file)>(time()-$this->time)||!$time)){
            $this->cache=false;
            print(file_get_contents($this->file));
        }
        else{
            $this->cache=true;
            ob_start();
        }
    }
    //Clining buffer and save content to file
    public function finish(){
        if($this->cache){
            $content=ob_get_clean();
            $file=fopen($this->file, 'w');
            fwrite($file, $content);
            print($content);
        }
    }
    //Clining buffer only
    public function crash(){
        if($this->cache){
            ob_clean();
        }
        print(file_get_contents($this->file));
    }
    //Remove saved file
    public function delete(){
        if(file_exists($this->file)){
            unlink($this->file);
        }
    }
    public function __set($key, $val){
        $this->$key=$val;
    }
    public function __get($key){
        return $this->$key;
    }
}
?>
